Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java StringBuilder Class

  • A mutable string of characters is represented by the Java class StringBuilder. The StringBuilder class offers a substitute for the String Class in Java because it creates a mutable sequence of characters instead of an immutable one like the String Class does. The functions of the StringBuilder and StringBuffer classes are very similar because both create mutable sequences of characters as an alternative to the String Class.

  • However, synchronization is where the StringBuilder class and StringBuffer class diverge. Contrary to the StringBuffer class, the StringBuilder class does not guarantee synchronization. As a result, this class is intended to be used in places where a single thread previously used the StringBuffer as a drop-in replacement (as is generally the case). It is advised to use this class instead of StringBuffer whenever possible because it will typically be faster in most implementations.





  • StringBuilder instances should not be used by multiple threads. The use of StringBuffer is advised if such synchronization is necessary. In comparison to String buffer, String Builder performs well but is not thread-safe.

Class hierarchy

java.lang.Object
 ↳ java.lang
    ↳ Class StringBuilder

Important Constructors of StringBuilder Class

  • StringBuilder() : StringBuilder() generates a blank StringBuilder with a 16-character initial capacity.
  • StringBuilder s = new StringBuilder();

  • StringBuilder(String str) : Creates a String Builder with the specified string by calling String Builder(String String_variable_name).
  • String Builder s = new StringBuilder(10);





  • StringBuilder(int capacity) : StringBuilder(int capacity) creates an empty String Builder with the capacity specified as its length.
  • StringBuilder s = new StringBuilder("DockerTpoint");

Important methods of StringBuilder class


Java String append() Methods


The append() method of the StringBuilder class concatenates the given argument with this string.


 // Java example of String length() Methods
public class Main {	
    public static void main(String args[]){  		 
      StringBuilder sb = new StringBuilder("Exam");
      sb.append("Deva"); //  original string is changed
      System.out.println(sb);
  } 
}



Output:

DockerTpoint 


Java String insert() Method


The insert() method of the StringBuilder class inserts the given string at the given position with this string.


 // Java example of String insert() Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuilder sb = new StringBuilder("Exam");
      sb.insert(4,"Deva"); //  original string is now changed
      System.out.println(sb);
  } 
}



Output:

DockerTpoint 

Java String delete() Method


The delete() method of the StringBuilder class remove the specified string from the begin Index and end Index.


 // Java example of String delete  Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuilder sb = new StringBuilder("DockerTpoint"); 
      sb.delete(0,4);  //   original string is now changed
      System.out.println(sb);
  } 
}



Output:

Deva 

Java String replace Method


The substring method of the StringBuilder class replaces the given string between begin Index and end Index-1.


 // Java example of String substring (int start, int end)  Method 
public class Main {	
    public static void main(String args[]){  		 
       StringBuilder sb = new StringBuilder("E  Deva"); 
       sb.replace(1,3,"xam");  // it will replace white space, original string is now changed
      System.out.println(sb);
  } 
}



Output:

DockerTpoint  

Java String reverse Method


The reverse method of the StringBuilder class reverses the current string.


 // Java example of String concat Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuilder sb = new StringBuilder("DockerTpoint"); 
      sb.reverse();
      System.out.println(sb);
  } 
}



Output:

aveDmaxE  

Java String capacity Method


The capacity method of the StringBuilder class returns the buffer's current capacity. The buffer's default capacity is sixteen. If the number of characters exceeds the current capacity, the capacity is increased by (oldcapacity*2)+2.


 // Java example of String indexOf Method 
public class Main {	
    public static void main(String args[]){  		 
      StringBuilder sb = new StringBuilder();
      System.out.println(sb.capacity()); // default value  16
      sb.append("DockerTpoint");
      System.out.println(sb.capacity()); // capicity is 16
      sb.append("I Love India");
      System.out.println(sb.capacity()); // Now (16*2)+2=34     i.e (oldcapacity*2)+2  
  } 
}



Output:

16 
16 
34 

Important methods of StringBuilder class

Important methods of StringBuilder class are mention below


Type and Modifier Method Description
public StringBuilder append(String s) Append(String s) is used to append the specified string to this string. The append() method has several overloads, including append(char), append(boolean), append(int), append(float), append(double)
public synchronized StringBuilder insert(int value, String s) The specific implementation of a method that is already provided by its parent class or superclass is granted by method overriding.
public synchronized StringBuilder replace(int begin Index, int end Index-1, String str) Replace is used to replace the string between the specified begin and end indexes.
public String substring(int begin Index, int end Index) substring returns the substring from the specified beginIndex and endIndex.
public String substring(int beginIndex) Substring returns the substring from the specified begin Index.
public int length() length() returns the length of the string, i.e. the total number of characters.
public char charAt(int index) charAt(int index) returns the character at the specified position.
public int capacity() capacity() returns the current capacity.
public void ensureCapacity(int minimumCapacity) ensureCapacity(int minimumCapacity) ensures that the capacity is at least equal to the specified minimum.
public StringBuilder reverse() The string can be reversed using reverse().
public StringBuilder delete(int start Index, int end Index) The string can be removed from the startIndex and endIndex by using the delete() method.
public void setCharAt(int index, char c) In setCharAt(int index, char c) method the character at the specified index is set to c.
public int String toString() The string returned by the string toString() method represents the data in this sequence.



Difference betweeen String and StringBuffer Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.